package com.bstek.code.view;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.vfs.FileNotFolderException;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;

import com.bstek.code.dao.DBInfoDao;
import com.bstek.code.model.TableInfo;
import com.bstek.code.service.templateservice.ITempService;
import com.bstek.code.service.templateservice.TempServiceFactory;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.core.Configure;

/**
 * 代码生成器，控制层
 * 
 * @author xh_hanhw
 *
 */
@Controller("codController")
public class CodeController {

	@Resource(name = DBInfoDao.BEAN_ID)
	private DBInfoDao dBInfoDao;

	/**
	 * 查询“我”的配置信息
	 * 
	 * @param parameter
	 * @return
	 * @throws Exception
	 */
	@DataProvider
	public Map<String, Object> getConfig(Map<String, Object> parameter) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("workSpaceSrc", Configure.getString("workSpaceSrc"));
		map.put("rootPkg", Configure.getString("rootPkg"));
		map.put("template", "1");
		return map;
	}

	/**
	 * 查询数据库表
	 * 
	 * @param parameter
	 * @throws Exception
	 */
	@DataProvider
	public List<TableInfo> queryTables(Map<String, Object> parameter) throws Exception {
		String tabName = "";
		if (parameter != null) {
			tabName = (String) parameter.get("tabName");
		}
		return dBInfoDao.getTables(tabName);
	}

	/**
	 * 读取指定目录，生成目录列表
	 * 
	 * @param parameter{workSpaceSrc,rootPkg,template,tabs}
	 * @return
	 * @throws Exception
	 */
	@DataProvider
	public List<Map<String, Object>> getDir(Map<String,Object> parameter) throws Exception {
		String workSpaceSrc= (String) parameter.get("workSpaceSrc");
		String rootPkg= (String) parameter.get("rootPkg");
		String template= (String) parameter.get("template");
		Assert.hasText(workSpaceSrc, "workSpaceSrc不能为空");
		Assert.hasText(rootPkg, "rootPkg不能为空");
		Assert.hasText(template, "template不能为空");
		//输入参数合法性检查
		File rootDir=new File(workSpaceSrc);
		if(rootDir.exists()){
			String pkgFilepath=StringUtils.replace(rootPkg, ".", "/");
			ITempService service = TempServiceFactory.getTempService(template);
			return service.generateMenuList(workSpaceSrc, pkgFilepath);
		}else{
			throw new FileNotFolderException("工作空间"+workSpaceSrc+"不存在，请检查!");
		}
	}

	/**
	 * 后台组装要生成的文件列表
	 * 
	 * @param parameter{template,tabs,fileType}
	 * @return
	 * @throws Exception
	 */
	@DataProvider
	public List<Map<String, Object>> getFile(Map<String,Object> parameter) throws Exception {
		String type= (String) parameter.get("type");
		String filePath= (String) parameter.get("filePath");
		String tabs= (String) parameter.get("tabs");
		String template= (String) parameter.get("template");
		Assert.hasText(type, "type不能为空");
		Assert.hasText(filePath, "filePath不能为空");
		Assert.hasText(tabs, "tabs不能为空");
		Assert.hasText(template, "template不能为空");
		
		ITempService service = TempServiceFactory.getTempService(template);
		return service.generateFileNames(tabs, filePath, type);
	}
	/**
	 * 根据模板生成文件
	 * @param parameter
	 * @return
	 * @throws Exception 
	 */
	@Expose
	public String createFile(Map<String,Object> parameter) throws Exception{
		//根据前台输入的参数，动态匹配模板，生成代码
		String template= (String) parameter.get("template");
		Assert.hasText(template, "template不能为空");
		//开始生成文件
		ITempService service = TempServiceFactory.getTempService(template);
		return service.create(parameter);
	}
	
}
